<template>
  <div class="app-container">
    <el-form :model="form" class="form-inline" ref="form" label-width="70px">
      <el-row :gutter="4">
        <el-col :lg="6" :xl="4"><el-form-item label="用户账号" style="width: 100%">
            <el-input v-model="form.userName" placeholder="用户账号"></el-input> </el-form-item></el-col>
        <el-col :lg="6" :xl="4"><el-form-item label="联系电话" style="width: 100%">
            <el-input v-model="form.phonenumber" placeholder="联系电话"></el-input> </el-form-item></el-col>
        <el-col :lg="6" :xl="4">
          <el-button type="primary" @click="getTableData">查询</el-button>
        </el-col>
      </el-row>
    </el-form>
    <el-row>
      <el-button type="primary" plain icon="el-icon-plus" @click="add" style="margin-bottom: 20px">添加用户账号</el-button>
    </el-row>
    <el-table ref="table" :data="tableData">
      <el-table-column align="center" prop="userName" label="用户账号"> </el-table-column>
      <!-- <el-table-column align="center" prop="password" label="密码"> </el-table-column> -->
      <el-table-column align="center" prop="phonenumber" label="手机号码"> </el-table-column>
      <el-table-column align="center" prop="companyName" label="公司名称"> </el-table-column>
      <el-table-column align="center" prop="loginDate" label="最后登录时间"> </el-table-column>
      <el-table-column align="center" prop="contactType" label="启用状态">
        <template slot-scope="scope">
          <el-switch :value="scope.row.status == '1'" @change="enableStatusChange(scope.row)" active-color="#13ce66" inactive-color="#ff4949">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="edit(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="del(scope.row.userId)">删除</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="editPassword(scope.row)">修改密码</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination style="margin-top: 20px" background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page.pageNum" :page-sizes="[10, 50, 100]"
      :page-size="page.pageSize" layout="->, total, sizes, prev, pager, next, jumper" :total="page.total">
    </el-pagination>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="40%" v-loading="loading">
      <el-form :model="addForm" ref="addForm" :rules="rules" label-width="140px">
        <el-form-item label="用户账号" style="width: 100%" prop="userName">
          <el-input v-model="addForm.userName" placeholder="用户账号"></el-input>
        </el-form-item>
        <el-form-item label="用户昵称" style="width: 100%" prop="nickName">
          <el-input v-model="addForm.nickName" placeholder="用户昵称"></el-input>
        </el-form-item>
        <el-form-item label="用户密码" style="width: 100%" prop="password" v-if="!editId">
          <el-input v-model="addForm.password" placeholder="用户密码"></el-input>
        </el-form-item>
        <el-form-item label="手机号码" style="width: 100%" prop="phonenumber">
          <el-input v-model="addForm.phonenumber" placeholder="手机号码"></el-input>
        </el-form-item>
        <el-form-item label="公司名称" style="width: 100%" prop="companyName">
          <el-input v-model="addForm.companyName" placeholder="公司名称"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cance">取 消</el-button>
        <el-button type="primary" @click="determine">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="修改密码" :visible.sync="showEditPassword" width="40%" v-loading="loading">
      <el-form :model="editPasswordForm" ref="editPasswordForm" :rules="rules" label-width="140px">
        <el-form-item label="用户密码" style="width: 100%" prop="password">
          <el-input v-model="editPasswordForm.password" placeholder="用户密码"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="() => showEditPassword = false">取 消</el-button>
        <el-button type="primary" @click="determineEditPassword">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      loading: false,
      page: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
      },
      editPasswordForm: {
        password: "",
      },
      title: "添加用户",
      editId: "",
      addForm: {
        userName: "",
        nickName: "",
        password: "",
        phonenumber: "",
        companyName: "",
      },
      dialogVisible: false,
      form: {
        userName: "",
        phonenumber: "",
      },
      tableData: [],
      tableHeight: 0,
      rules: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        nickName: [{ required: true, message: "请输入昵称", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        phonenumber: [
          { required: true, message: "请输入手机号", trigger: "blur" },
        ],
        companyName: [
          { required: true, message: "请输入公司名称", trigger: "blur" },
        ],
      },
      showEditPassword: false,
      row: {},
    };
  },
  methods: {
    enableStatusChange(row) {
      const params = {
        userId: row.userId,
      };
      if (row.status == "0") {
        this.$post(`/system/monitoruser/enable`, params).then((res) => {
          if (res && res.code * 1 === 200) {
            this.$message({
              type: "success",
              message: "启用成功!",
            });
            this.getTableData();
          }
        });
      } else {
        this.$post(`/system/monitoruser/disable`, params).then((res) => {
          if (res && res.code * 1 === 200) {
            this.$message({
              type: "success",
              message: "禁用成功!",
            });
            this.getTableData();
          }
        });
      }
    },
    editPassword(row) {
      this.editPasswordForm.password = "";
      this.showEditPassword = true;
      this.row = row;
    },
    determineEditPassword() {
      const params = {
        userId: this.row.userId,
        password: this.editPasswordForm.password,
      };
      this.$refs.editPasswordForm.validate((valid) => {
        if (valid) {
          if (this.loading) {
            return;
          }
          this.loading = true;
          this.$post(`/system/monitoruser/updatePwd`, params)
            .then((res) => {
              if (res && res.code * 1 === 200) {
                this.$message({
                  type: "success",
                  message: "操作成功!",
                });
                this.showEditPassword = false;
                this.getTableData();
              }
            })
            .finally(() => {
              this.loading = false;
            });
        }
      });
    },
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.getTableData();
    },
    handleCurrentChange(val) {
      this.page.pageNum = val;
      this.getTableData();
    },
    setTableHeight() {
      this.$nextTick(() => {
        const queryConditionHeight = this.$refs.form.$el.offsetHeight;
        this.tableHeight =
          window.innerHeight - queryConditionHeight - 180 + "px";
      });
    },
    determine() {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          if (this.loading) {
            return;
          }
          this.loading = true;
          const url = this.editId ? "system/monitoruser" : "system/monitoruser";
          const func = this.editId ? "$put" : "$post";
          const params = this.editId
            ? { ...this.addForm, userId: this.editId }
            : this.addForm;
          this[func](url, params)
            .then((res) => {
              if (res && res.code * 1 === 200) {
                this.$message({
                  type: "success",
                  message: this.editId ? "修改成功" : "新增成功",
                });
                this.$refs.addForm.resetFields();
                this.dialogVisible = false;
                this.getTableData();
              }
            })
            .finally(() => {
              this.loading = false;
            });
        }
      });
    },
    del(userId) {
      this.$confirm("是否确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          if (this.loading) {
            return;
          }
          this.loading = true;
          this.$delete(`/system/monitoruser/${userId}`).then((res) => {
            if (res && res.code * 1 === 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getTableData();
            }
          });
        })
        .catch(() => {
          //
        })
        .finally(() => {
          this.loading = false;
        });
    },
    cance() {
      this.$refs.addForm.resetFields();
      this.dialogVisible = false;
    },
    add() {
      this.$nextTick(() => {
        this.title = "添加用户";
        this.dialogVisible = true;
        this.addForm.userName = "";
        this.addForm.nickName = "";
        this.addForm.password = "";
        this.addForm.phonenumber = "";
        this.addForm.companyName = "";
        this.editId = "";
      });
    },
    edit(row) {
      this.$nextTick(() => {
        this.title = "修改用户";
        this.dialogVisible = true;
        this.addForm.userName = row.userName;
        this.addForm.nickName = row.nickName;
        this.addForm.password = row.password;
        this.addForm.phonenumber = row.phonenumber;
        this.addForm.companyName = row.companyName;
        this.editId = row.userId;
      });
    },
    getTableData() {
      const params = {
        ...this.form,
        pageNum: this.page.pageNum,
        pageSize: this.page.pageSize,
      };
      this.$get("/system/monitoruser/list", params).then((res) => {
        this.tableData = res.rows;
      });
    },
  },
  mounted() {
    window.onresize = () => {
      this.setTableHeight();
    };
    this.setTableHeight();
    this.getTableData();
  },
};
</script>
<style scoped lang="scss">
.form-inline ::v-deep {
  width: 100%;

  .el-form-item__label {
    // padding: 0;
  }
}
</style>